package com.tulei.qqread;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;



public class main {

    private static final String DB_URL = "jdbc:mysql://localhost:3306/admin";
    private static final String USER = "root";
    private static final String PASS = "123456";
    public static void main(String[] args) throws SQLException {

        //获取数据 （5页）
        List<Book> books = QQReadUtil.getData(2);

        // 保存数据库
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS)) {

            // Insert books
            String sqlInsertBook = "INSERT INTO book (title, author, kind, intro,serial,count) VALUES (?, ?, ?, ?,?,?)";
            try (PreparedStatement pstmtInsertBook = conn.prepareStatement(sqlInsertBook)) {
                for (Book book : books) {
                    pstmtInsertBook.setString(1, book.getTitle());
                    pstmtInsertBook.setString(2, book.getAuthor());
                    pstmtInsertBook.setString(3, book.getKind());
                    pstmtInsertBook.setString(4, book.getIntro());
                    pstmtInsertBook.setString(5, book.getSerial());
                    pstmtInsertBook.setString(6, book.getCount());
                    pstmtInsertBook.executeUpdate();
                }
                System.out.println("Books inserted successfully!");
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
